package team.zero.lemon.common.utils;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.List;
import java.util.Map;

/**
 * Created by bolin on 2016/5/23.
 */
public class LogicCalculation {

    /**
     * @param logic "(a >= 0 && a <= 5)"
     * @return
     */
    public static boolean compute(String logic, Map<String, Object> parameters) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        parameters.forEach((parameter,object)-> {
            engine.put(parameter,object);
        });
        Object result = engine.eval(logic);
        return (boolean) result;
    }
}
